package com.biz.primus.management.commodity.controller;

import com.biz.primus.commodity.vo.SaleTagQueryVo;
import com.biz.primus.commodity.vo.backend.SaleTagListItemVo;
import com.biz.primus.commodity.vo.saleTag.SaleTagVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.commodity.feign.SaletagFeignClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * @author jiangnan
 * @Description: TODO
 * @create 2017-10-12-下午2:19
 */
@Controller
@RequestMapping("management/saletag")
public class SaletagController extends AbstractBaseController {

    private final SaletagFeignClient saletagFeignClient;

    public SaletagController(SaletagFeignClient saletagFeignClient) {
        this.saletagFeignClient = saletagFeignClient;
    }

    @GetMapping(value = "list")
    @ResponseBody
    @PreAuthorize("hasAuthority('OPT_SALETAG_LIST')")
    public ModelAndView list(@ModelAttribute("reqVo") SaleTagQueryVo reqVo) {
        SpringPageVO<SaleTagListItemVo> result = saletagFeignClient.findAll(reqVo);
        return new ModelAndView("saletag/list", "pages", result);
    }

    @RequestMapping("{id}")
    @PreAuthorize("hasAuthority('OPT_SALETAG_DETAIL')")
    public ModelAndView edit(@PathVariable Long id) {
        return new ModelAndView("saletag/detail")
                .addObject("saletag", saletagFeignClient.getSaleTagById(id));

    }

    @RequestMapping("create")
    @PreAuthorize("hasAnyAuthority('OPT_SALETAG_CREATE')")
    public ModelAndView create() {
        return new ModelAndView("saletag/detail")
                .addObject("saletag", new SaleTagVo());
    }

    @PostMapping("save")
    @PreAuthorize("hasAuthority('OPT_SALETAG_UPDATE')")
    public ModelAndView save(@Valid SaleTagVo saleTagVo, BindingResult result) {
        error(result);
        saletagFeignClient.update(saleTagVo);
        return new ModelAndView("redirect:/management/saletag/list");
    }


}
